<template>
	<view class="content" :style="'background-image: url('+url+'/addon/qianyu_petrol/wxapp/image/lqzx.jpg'"
		style="box-sizing: border-box;background-size: 100% 100%;">
		<view class="wid100" style="height: 476rpx;"></view>
		<scroll-view style="height: calc(100vh - 476rpx);" class="wid100" scroll-y="true" @scrolltolower="lower">
			<view class="hei20"></view>
			<view v-if="list.length == 0" class="wid90 mar fowe800 foSi35 bacFFF borRad20" style="padding: 20rpx 0;">
				<view style="height: 50rpx;"></view>
				<view class="dis disJuC">
					<image src="../../static/zanwu1.png" style="width: 400rpx;height: 300rpx;"></image>
				</view>
				<view class="textCen mar-top20" style="color: #868686;">暂无数据</view>
			</view>
			<block v-for="(item,index) in list" :key="index">
				<view class="mar mar-bott20 dis coFFF poRel wid85" :style="'background-image: url('+url+'/addon/qianyu_petrol/wxapp/image/yhq_red.png);'"
				style="height: 190rpx;box-sizing: border-box;background-size: 100% 100%;">
					<view class="dis disAl disJuC" style="width: 180rpx;">
						<view class="foSi30 fowe800" style="vertical-align: bottom;color: #fff;">
							<span>¥</span>
							<span class="fowe600 foSi40">{{item.money}}</span>
						</view>
					</view>
					<view class="dis disAl mar-left30">
						<view class="textCen" style="width: 222rpx;">
							<view class="foSi25">{{item.title}}</view>
							<view class="foSi20 mar-top10 ">领取7天内有效</view>
						</view>
					</view>
					<view class="poAbs hei60 borRad40 foSi30 fowe600 dis disAl disJuC butt1" 
						v-if="item.receive_type == 0" @click="lingqu(item)">立即领取</view>
					<view class="poAbs hei60 borRad40 foSi30 fowe600 dis disAl disJuC butt1" @click="tiaozhuan('/pages/toiletries/toiletries')"
						v-if="item.receive_type == 1 || item.receive_type == 2">去使用</view>
					<view v-if="item.receive_type == 1 || item.receive_type == 2" class="poAbs" style="top: -2rpx;left: -2rpx;width: 89rpx;height: 82rpx;">
						<image src="../../static/yelingqu.png" class="wh100"></image>
					</view>
				</view>
			</block>
			<view style="height: 50rpx;"></view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				list: [],
				last_page: 0,
				page: 1,
			}
		},
		onLoad() {
			this.getList()
		},
		methods: {
			dateFormat(time) {
				let date = new Date(time*1000);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1;
				// let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				return month+". "+day;
				// return year + "-" + month + "-" + day;
			},
			dingyue(item) {
				var that = this
				// 要检查的订阅idrecharge_template
				let id = uni.getStorageSync('tzId').recharge_template
				let id1 = uni.getStorageSync('tzId').refund_template
				let id2 = uni.getStorageSync('tzId').coupon_is_to_expire_template
				// 这里是获取下发权限地方，根据官方文档，可以根据  wx.getSetting() 的 withSubscriptions   这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
				wx.getSetting({
					withSubscriptions: true, //  这里设置为true,下面才会返回mainSwitch
					success: function(res) {
						// 调起授权界面弹窗
						// if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
						// 	if (res.subscriptionsSetting.itemSettings !=
						// 		null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
						// 		that.lingqu(item)
						// 	} else {
								// 当用户没有点击 ’总是保持以上选择，不再询问‘  按钮。那每次执到这都会拉起授权弹窗
								wx.showModal({
									title: '提示',
									content: '请授权开通服务通知',
									showCancel: true,
									success: function(ress) {
										if (ress.confirm) {
											wx.requestSubscribeMessage({ // 调起消息订阅界面
												tmplIds: [id,id1,id2],
												success(res) {
													console.log('订阅消息 成功 ');
													console.log(res);
													that.lingqu(item)
												},
												fail(er) {
													console.log("订阅消息 失败 ");
													console.log(er);
													that.lingqu(item)
												}
											})
										}
									}
								})
						// 	}
						// } else {
						// 	console.log('订阅消息未开启')
						// 	that.lingqu(item)
						// }
					},
					fail: function(error) {
						console.log(error);
						that.lingqu(item)
					},
				})
			},
			lingqu(item){
				var that = this;
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_coupon_receive',
					method: "POST",
					header: {
						'site-id': that.global.site_id,//自定义请求头信息
						'token': uni.getStorageSync('token')
					},
					data: {
						gas_station_id: uni.getStorageSync('jiaId'),
						coupon_id: item.id,
						active_id: item.active_id
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							that.page = 1
							that.list = []
							that.getList()
						}
					}
				})
			},
			getList() {
				var that = this;
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_coupon_list',
					method: "POST",
					header: {
						'site-id': that.global.site_id,//自定义请求头信息
						'token': uni.getStorageSync('token')
					},
					data: {
						gas_station_id: uni.getStorageSync('jiaId'),
						types: '',
						page: that.page,
						limit: 10
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							that.last_page = result.data.last_page
							that.list = that.list.concat(result.data)
						}
					}
				})
			},
			lower(e) {
				if(this.last_page > this.page){
					this.page++
					this.getList()
				}
			},
			tiaozhuan(url){
				uni.switchTab({
					url: url
				})
			}
		}
	}
</script>

<style>
	.content {
		height: 100vh;
	}
	.butt1{
		right: 20rpx;
		top: 65rpx;
		width: 184rpx;
		background-color: #FFB969;
		color: #FC1E1B;
	}
</style>
